﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using MS.TravelJourney.Final.Resources;
using Windows.ApplicationModel;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;

namespace MS.TravelJourney.Final.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        #region Members
        private const string TXTFILEEXTENSION = "txt";
        #endregion
        #region Construction
        public MainViewModel()
        {
            this.Items = new ObservableCollection<ItemViewModel>();
        }
        #endregion
        
        /// <summary>
        /// ItemViewModel 对象的集合。
        /// </summary>
        public ObservableCollection<ItemViewModel> Items { get; private set; }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// 创建一些 ItemViewModel 对象并将其添加到 Items 集合中。
        /// </summary>
        //public void LoadData()
        //{
        //    var path = Path.GetFullPath("Data/丽江/");
        //    var files = new DirectoryInfo(path).GetFiles();
        //    foreach (var file in files)
        //    {
        //        string filename = file.Name.Split('.')[0];
        //        if (file.Extension.Equals(TXTFILEEXTENSION))
        //        {
        //            var item = this.Items[this.Items.Count - 1];
        //            var stream = file.Open(FileMode.Open);
        //            var streamReader = new StreamReader(stream);
        //            item.Desc = streamReader.ReadToEnd();
        //            this.Items[this.Items.Count - 1] = item;
        //        }
        //        else
        //        {
        //            var item = new ItemViewModel
        //            {
        //                Name = filename,
        //                Photo = "/Data/丽江/" + file.Name
        //            };
        //            this.Items.Add(item);
        //        }
        //    }
        //    this.IsDataLoaded = true;
        //}
        public async Task<bool> LoadData()
        {
            try
            {
                var folderRoot = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Data");
                var folder = await folderRoot.GetFolderAsync("丽江");

                foreach (var file in await folder.GetFilesAsync())
                {
                    string filename = file.Name.Split('.')[0];
                    string extension = file.Name.Split('.')[1];
                    if (extension.Equals(TXTFILEEXTENSION))
                    {
                        var item = this.Items[this.Items.Count - 1];
                        var stream = await file.OpenAsync(FileAccessMode.Read);
                        var streamReader = new StreamReader(stream.AsStream());
                        item.Desc = streamReader.ReadToEnd();
                        this.Items[this.Items.Count - 1] = item;
                    }
                    else
                    {
                        var item = new ItemViewModel
                        {
                            Name = filename,
                            Photo = "/Data/丽江/" + file.Name
                        };
                        App.ViewModel.Items.Add(item);
                    }
                }
                App.ViewModel.IsDataLoaded = true;
                return true;
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}